package com.yoga.controller;

import com.yoga.entity.YogaUser;
import com.yoga.entity.YogaVenue;
import com.yoga.service.VenueService;
import com.yoga.service.YogaUserService;;
import com.yoga.utils.ResultVO;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class BossController {

    @Resource
    VenueService venueService;
    @Resource
    YogaUserService yogaUserService;


    //超级管理员查询所有场馆管理用户
    @RequestMapping("findAllVenueUser")
    public ResultVO findAllVenueUser(){
        List<YogaVenue> venueList = venueService.findAllVenue();
        List<Integer> uidList = null;
        for (YogaVenue yogaVenue:venueList) {
            Integer uid = yogaVenue.getUid();
            uidList.add(uid);
        }
        List <YogaUser> userList = null;
        for (Integer uid:uidList) {
            YogaUser user = yogaUserService.findOne(uid);
            userList.add(user);
        }
        return ResultVO.success("查询场馆管理成功",userList);
    }

    @RequestMapping("addNewVenue")
    public ResultVO addNewVenue(@RequestBody YogaVenue yogaVenue){
        System.out.println("进入了新增场馆请求");
        YogaUser user = new YogaUser();
        user.setUname(yogaVenue.getTel());
        user.setIntroduction(yogaVenue.getVname());
        user.setPhone(yogaVenue.getTel());
        user.setUpwd("888888");
        venueService.addVenue(yogaVenue);
        yogaUserService.addUser(user);
        return ResultVO.success("新建场馆成功");
    }

    @RequestMapping("activeVenue")
    public ResultVO activeVenue(Integer vid){
        YogaVenue venue = venueService.findVenueByVid(vid);
        String tel = venue.getTel();
        YogaUser user = yogaUserService.findUserByPhone(tel);
        Integer uid = user.getUid();
        venue.setUid(uid);
        Boolean aBoolean = venueService.activeVenue(venue);
        if (aBoolean){
            return ResultVO.success("激活成功");
        }
        return ResultVO.fail("激活失败");
    }

    @RequestMapping("changeVenueUser")
    public ResultVO changeVenueUser(Integer vid,String phone){
        YogaVenue yogaVenue = venueService.findVenueByVid(vid);
        YogaUser yogaUser = yogaUserService.findUserByPhone(phone);
        Integer uid = yogaUser.getUid();
        yogaUser.setIntroduction(yogaVenue.getVname());
        yogaVenue.setTel(phone);
        yogaVenue.setUid(uid);
        Boolean flag = venueService.changeVenueUser(yogaVenue, yogaUser);
        if(flag) {
            return ResultVO.fail("变更场馆管理用户成功");
        }
        return ResultVO.fail("变更场馆管理用户失败");
    }
}
